package com.eshop.tools.rest;

import org.springframework.web.bind.annotation.PostMapping;
import com.eshop.tools.domain.vo.EmailVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PutMapping;
import com.eshop.logging.aop.log.Log;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.validation.annotation.Validated;
import com.eshop.tools.domain.EmailConfig;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import com.eshop.tools.service.EmailConfigService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({ "api/email" })
@Api(tags = { "工具：邮件管理" })
public class EmailController
{
    private final EmailConfigService emailService;
    
    public EmailController(final EmailConfigService emailService) {
        this.emailService = emailService;
    }
    
    @GetMapping
    public ResponseEntity<Object> get() {
        return (ResponseEntity<Object>)new ResponseEntity((Object)this.emailService.find(), HttpStatus.OK);
    }
    
    @Log("配置邮件")
    @PutMapping
    @ApiOperation("配置邮件")
    public ResponseEntity<Object> emailConfig(@Validated @RequestBody final EmailConfig emailConfig) {
        this.emailService.update(emailConfig, this.emailService.find());
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
    
    @Log("发送邮件")
    @PostMapping
    @ApiOperation("发送邮件")
    public ResponseEntity<Object> send(@Validated @RequestBody final EmailVo emailVo) throws Exception {
        this.emailService.send(emailVo, this.emailService.find());
        return (ResponseEntity<Object>)new ResponseEntity(HttpStatus.OK);
    }
}
